
package com.xiaoma.tpo.schedule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.xiaoma.tpo.dao.LiveListDao;
import com.xiaoma.tpo.domain.LiveList;
import com.xiaoma.tpo.utils.baidupush.AndroidPushMsg;
import com.xiaoma.tpo.utils.baidupush.BDPush;
import com.xiaoma.tpo.utils.baidupush.IOSPushMsg;

import net.sf.json.JSONObject;

/**
 * Created by Owner on 2015/5/15.
 */
@Component("liveTask")
public class LiveListTask {

    @Autowired
    private LiveListDao liveListDao;

    public void init(){

        setStart();
        setEnd();
    }

    //查询当前时间内的开始直播
    public void setStart(){
        LiveList ll = new LiveList();
        List<LiveList> list = liveListDao.getStartLiveList();
        if(list.size()>0){
            ll = list.get(0);
            ll.setStatus(1);
            liveListDao.update(ll);
            pushMsgIos(ll);
            pushMsgAndroid(ll);
        }
    }

    //修改过期直播的状态
    public void setEnd(){
        LiveList ll = new LiveList();
        List<LiveList> list = liveListDao.getEndLiveList();
        if(list.size()>0){
            ll=list.get(0);
            ll.setStatus(2);
            liveListDao.update(ll);
        }
    }

    
    //IOS 广播推送
    public void pushMsgIos(LiveList live) {
        IOSPushMsg msg = new IOSPushMsg();
        msg.setTitle(live.getLiveTitle());
        Map<String, Object> content = new HashMap<String, Object>();
        content.put("liveId", live.getLiveId());
        content.put("flag", 2);
        content.put("title","托福21天");
        msg.setContent(content);
        try {
            BDPush.iosPush2All(msg);
        } catch (PushClientException | PushServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //Android 广播推送
    public void pushMsgAndroid(LiveList live) {
        AndroidPushMsg androidMsg = new AndroidPushMsg();
        androidMsg.setTitle("托福21天");
        androidMsg.setDescription(live.getLiveTitle());
        JSONObject custom = new JSONObject();
        custom.put("liveId",live.getLiveId());
        custom.put("flag",2);
        androidMsg.setCustomContent(custom);
        try {
            BDPush.androidPush2All(androidMsg);
        } catch (PushClientException | PushServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String args[]) {
        LiveListTask lt = new LiveListTask();
        LiveList ll = new LiveList();
        ll.setLiveId("hhhhhh");
        ll.setLiveTitle("ssssssssssss");
        ll.setStatus(1);
        lt.pushMsgIos(ll);
    }

}



